package com.tarena.lbs.marketing.web.repository.impl;


import com.github.pagehelper.PageInfo;
import com.tarena.lbs.marketing.web.mapper.CouponMapper;
import com.tarena.lbs.marketing.web.mapper.UserReceiveCouponMapper;
import com.tarena.lbs.marketing.web.repository.UserReceiveCouponRepository;
import com.tarena.lbs.base.protocol.pager.BasePageList;
import com.tarena.lbs.common.marketing.converter.UserReceiveCouponConverter;
import com.tarena.lbs.common.marketing.enums.CouponCodeStatusEnum;
import com.tarena.lbs.common.passport.principle.UserPrinciple;
import com.tarena.lbs.common.security.utils.LbsSecurityContenxt;
import com.tarena.lbs.pojo.marketing.bo.UserReceiveCouponBO;
import com.tarena.lbs.pojo.marketing.dos.CouponDO;
import com.tarena.lbs.pojo.marketing.dos.UserReceiveCouponDO;
import com.tarena.lbs.pojo.marketing.param.UpdateParam;
import com.tarena.lbs.pojo.marketing.param.UserReceiveCouponParam;
import com.tarena.lbs.pojo.marketing.query.UserCouponCodeQuery;
import com.tarena.lbs.pojo.marketing.query.UserCouponQuery;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
import javax.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

@Service
public class UserReceiveCouponRepositoryImpl implements UserReceiveCouponRepository {

    @Resource
    private CouponMapper couponMapper;

    @Resource
    private UserReceiveCouponMapper userReceiveCouponMapper;

    @Resource
    private UserReceiveCouponConverter userReceiveCouponConverter;

    @Override
    public void updateStatus(UpdateParam updateParam) {
        UserReceiveCouponDO tempDO = new UserReceiveCouponDO();
        tempDO.setId(updateParam.getId());
        tempDO.setStatus(updateParam.getStatus());
        tempDO.setUpdateAt(new Date());
        userReceiveCouponMapper.updateByPrimaryKeySelective(tempDO);
    }

    @Override
    public void insert(UserReceiveCouponParam param) {
        CouponDO couponDO = couponMapper.selectByPrimaryKey(param.getCouponId());
        UserReceiveCouponDO tempDO = new UserReceiveCouponDO();
        BeanUtils.copyProperties(param,tempDO);
        tempDO.setCreateAt(new Date());
        tempDO.setStatus(CouponCodeStatusEnum.ENABLE.getCode());
        tempDO.setCouponType(couponDO.getCouponType());
        tempDO.setCouponValue(couponDO.getDiscountValue());
        tempDO.setShopId(param.getShopId());
        userReceiveCouponMapper.insert(tempDO);
    }

    @Override
    public BasePageList<UserReceiveCouponBO> userList(UserCouponQuery couponQuery) {
        UserPrinciple loginUser = LbsSecurityContenxt.getLoginToken();
        if (loginUser != null) {
            couponQuery.setUserId(loginUser.getId());
        }
        int page = couponQuery.getPageNo();
        int pageSize = couponQuery.getPageSize();
        //new PageQuery(page, pageSize);
        List<UserReceiveCouponDO> list = userReceiveCouponMapper.selectUserList(couponQuery);
        PageInfo<UserReceiveCouponDO> pageInfo = new PageInfo<>(list);
        List<UserReceiveCouponBO> couponBOList = pageInfo.getList().stream().map(storeDO -> {
            UserReceiveCouponBO storeBO = new UserReceiveCouponBO();
            BeanUtils.copyProperties(storeDO, storeBO);
            return storeBO;
        }).collect(Collectors.toList());
        return new BasePageList<UserReceiveCouponBO>(couponBOList,Long.valueOf(pageInfo.getTotal()));
    }

    @Override
    public List<UserReceiveCouponBO> userCouponList(Integer userId, Integer couponId) {
        return userReceiveCouponConverter.dos2bos(userReceiveCouponMapper.userCouponList(userId,couponId));
    }

    @Override
    public UserReceiveCouponBO userCouponDetail(UserCouponCodeQuery query) {
        return userReceiveCouponConverter.do2bo(userReceiveCouponMapper.userCouponDetail(query));
    }

    @Override
    public void update(UserReceiveCouponParam param) {

    }
}
